package com.lg.online_shop.controller;

import com.lg.online_shop.entity.Cart;
import com.lg.online_shop.entity.Color;
import com.lg.online_shop.entity.Good;
import com.lg.online_shop.entity.Size;
import com.lg.online_shop.mapper.CartMapper;
import com.lg.online_shop.mapper.ColorMapper;
import com.lg.online_shop.mapper.GoodMapper;
import com.lg.online_shop.mapper.SizeMapper;
import com.lg.online_shop.utils.Result;
import com.lg.online_shop.vo.CartInfo;
import com.lg.online_shop.vo.CartInfoList;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {


    @Resource
    private CartMapper cartMapper;
    @Autowired
    private SizeMapper sizeMapper;
    @Autowired
    private ColorMapper colorMapper;
    @Resource
    GoodMapper goodMapper;

    @GetMapping("/info")
    public Result getCartInfo(Long userId) {
        List<CartInfo> cartItems = cartMapper.selectCartInfoList(userId);

        for (CartInfo cartItem : cartItems) {
            List<Size> sizes = sizeMapper.selectSizeBySizeId(cartItem.getSizeId());
            cartItem.setSizeName(sizes.get(0).getSize());
            List<Color> colors = colorMapper.selectColorByColorId(cartItem.getColorId());
            cartItem.setColorName(colors.get(0).getColor());
        }

        double totalPrice = 0;
        for (CartInfo cartItem : cartItems) {
            totalPrice += cartItem.getCount() * cartItem.getGPrice();
        }
        CartInfoList cartData = new CartInfoList(cartItems, totalPrice);
        return Result.success(cartData);
    }

    //    添加购物车
    @GetMapping("/add")
    public Result addCart(long userId, long goodId, long colorId, long sizeId, int count) {
        Cart cart = new Cart(null, goodId, userId, colorId, sizeId, count);
//        判断库存是否足够
        Good good = goodMapper.selectById(goodId);
        Integer stock = good.getGStock();
        if (stock < count) {
            return Result.error("库存不足");
        }
//        good.setGStock(stock - count);
//        判断是否存在，存在 count 加一
        Cart existCart = cartMapper.findOne(cart);

        if (existCart != null) {
            existCart.setCount(existCart.getCount() + count);
            cartMapper.updateById(existCart);
            goodMapper.updateById(good);
            return Result.success("添加成功");
        }
        cartMapper.insertCart(cart);
        goodMapper.updateById(good);
        return Result.success("添加成功");
    }

    //    修改商品数量
    @GetMapping("/updateCount")
    public Result updateCart(Long id, Integer count) {
//        判断是否存在
        Cart cart1 = cartMapper.selectById(id);
        if (cart1 == null) {
            return Result.error("商品不存在");
        }
        Cart cart = new Cart(id, null, null, null, null, count);
        cartMapper.updateCount(cart);
        return Result.success("修改成功");
    }

//    购买购物车
    @GetMapping("/buyCart")
    public Result buyCart(Long userId) {

        return  Result.success();
    }
}